package com.wn.common.utils;

import com.wn.common.exception.Assert;
import com.wn.common.model.vo.TokenVo;
import com.wn.common.result.enums.impl.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author yellow Docter
 * @date 2023 -07-27
 * @desc
 * 通过工具类获取tokenVo
 */
public class TokenUtil {

    /**
     * 获取tokenVo 对象
     * @return
     */
    public static TokenVo getTokenVo(){

        //如何获取 请求头中的信息
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        String userId = request.getHeader("userId");
        String phone = request.getHeader("phone");

        //没有数据 抛出异常
        Assert.error(StringUtils.isEmpty(userId), BusinessCode.NO_AUTH);

        TokenVo tokenVo = new TokenVo();
        tokenVo.setUserId(userId);
        tokenVo.setPhone(phone);
        return tokenVo;
    }

}
